package com.company.statistician;

import com.company.exception.NoMatchException;
import com.company.graphgetter.GraphGetter;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 内部继承了抽象类 EdgeStatistician
 * 抽象类中有变量 List<TrianglePacket> edgePackets;
 * 这个packets是一个副本 可以放心使用
 */
public class EdgeDistributedColumnAccumulateStatistician extends EdgeStatistician {
    int barrelSize;
    int maxBarrelIndex;
    int theta;
    HashMap<Integer, Integer> barrels;

    public EdgeDistributedColumnAccumulateStatistician(GraphGetter graphGetter, int barrelSize, int theta) throws NoMatchException {
        super(graphGetter);
        this.barrelSize = barrelSize;
        this.barrels = new HashMap<>();
        this.theta = theta;
    }

    @Override
    public void calcData() {
        for (int i = 0; i < edgePackets.size(); i++) {
            addIntoBarrel(edgePackets.get(i).getValue());
        }
    }

    @Override
    public Object outputData() {
        int sum = 0;
        ArrayList<Column> columns = new ArrayList<>();
        for (int barrelIndex = 0; barrelIndex <= maxBarrelIndex; barrelIndex++) {
            Integer barrelCount = barrels.getOrDefault(barrelIndex, 0);
            sum += barrelCount;
//            if ((barrelIndex) * barrelSize < theta) {
//                Column column = new Column(barrelIndex * barrelSize, barrelSize, sum);
//                System.out.println(column);
//                columns.add(column);
//            }
            Column column = new Column(barrelIndex * barrelSize, barrelSize, sum);
            columns.add(column);
        }
        return columns;
    }

    private void addIntoBarrel(int value) {
        int barrelIndex = value / barrelSize;
        if (barrelIndex > maxBarrelIndex) {
            maxBarrelIndex = barrelIndex;
        }
        barrels.put(barrelIndex, barrels.getOrDefault(barrelIndex, 0) + 1);
    }

}
